package com.itheima.reggie.controller.test;


import com.itheima.reggie.view.ExcelView;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/test/yhans")
public class MyController {

    @GetMapping("/downloadExcel")
    public ResponseEntity<byte[]> downloadExcel() throws IOException {
        // 从数据库或其他数据源中获取数据
        List<Map<String, String>> dataList = getDataFromDataSource();

        // 创建Workbook对象并生成Excel文件
        Workbook workbook = createExcel(dataList);

        // 将Workbook转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);
        byte[] excelBytes = baos.toByteArray();

        // 设置响应头信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        headers.setContentDispositionFormData("attachment", "data.xlsx");

        return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);
    }

    private Workbook createExcel(List<Map<String, String>> dataList) {
        // 创建工作簿等操作，参考上面的ExcelView类
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Data");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        int columnIndex = 0;
        for (String key : dataList.get(0).keySet()) {
            Cell cell = headerRow.createCell(columnIndex);
            cell.setCellValue(key);
            columnIndex++;
        }

        // 填充数据
        int rowIndex = 1;
        for (Map<String, String> dataMap : dataList) {
            Row dataRow = sheet.createRow(rowIndex);
            columnIndex = 0;
            for (String value : dataMap.values()) {
                Cell cell = dataRow.createCell(columnIndex);
                cell.setCellValue(value);
                columnIndex++;
            }
            rowIndex++;
        }


        return workbook;
    }

    private List<Map<String, String>> getDataFromDataSource() {
        // 从数据库或其他数据源中获取数据

        List<Map<String, String>> dataList = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("like", "水浒传");
        map.put("eat", "海底捞");
        map.put("sport", "游戏");
        dataList.add(map);

        return dataList;
    }
}
